<template>
  <base-breadcrumb :title="titleName">
    <searchData :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <!-- <title-name title="双优化列表" /> -->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :selectConfig = "{use: true, type: 'checkbox',rowKey: 'optimizationId' }"
        :exportConfig="{ use: true,select: '双优化实施登记',all: '双优化实施登记', customColumns: customColumns}"
        :is-show-count-bar="false"
      >
        <template slot="bottom">
          <div class="pub-table-select-count">
            <a-icon
              type="info-circle"
              theme="twoTone"
            />
            <span style="margin-right: 12px">
          双优化总数：<span class="totalNum">{{ totalDels.total }} 个</span> |
          实施中 ：<span class="totalNum">{{ totalDels.underImplementNum }} 个</span> |
          未实施：<span class="totalNum">{{ totalDels.unImplementNum }} 个</span> |
          销项审核中：<span class="totalNum">{{ totalDels.cancellationAuditNum }} 个</span> |
          销项审核不通过：<span class="totalNum">{{ totalDels.cancellationNoPassNum }} 个</span> |
           销项审核通过：<span class="totalNum">{{ totalDels.cancellationPassNum }} 个</span>
        </span>
            <span
              style="float:right;"
              class="totalNum"
              @click="toggle"
            >
          {{ expand === true ? '展开详细汇总信息' : '收起详细汇总信息' }}<a-icon :type="expand ? 'down' : 'up'" />
        </span>
          </div>
          <div v-if="expand === false" class="wrapper">
            <div class="row">
              <div class="name">
                立项总效益
              </div>
              <div class="name">
                实施中，立项效益
              </div>
              <div class="name">
                未实施，立项效益
              </div>
              <div class="name">
                销项审核中，实施效益
              </div>
              <div class="name">
                销项审核不通过，实施效益
              </div>
              <div class="name">
                销项审核通过，实施效益
              </div>
            </div>
            <div class="row">
              <div class="name">
                {{ benefitTotals.totalBenefits }}
              </div>
              <div class="name">
                {{ benefitTotals.underImplementBenefits }}
              </div>
              <div class="name">
                {{ benefitTotals.unImplementBenefits }}
              </div>
              <div class="name">
                {{ benefitTotals.cancellationAuditBenefits }}
              </div>
              <div class="name">
                {{ benefitTotals.cancellationNoPassBenefits }}
              </div>
              <div class="name">
                {{ benefitTotals.cancellationPassBenefits }}
              </div>
            </div>
          </div>
        </template>
        <template slot="serial" slot-scope="text, record, index">{{ index + 1 }}</template>
<!--        <template slot="type" slot-scope="text, record, index">-->
<!--          <span v-if="record.type== '111'">设计优化</span>-->
<!--          <span v-if="record.type== '222'">施工组织设计(方案)优化</span>-->
<!--        </template>-->
        <template slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
        </template>
        <template slot="description" slot-scope="text">
          <ellipsis :length="8" tooltip>{{ text }}</ellipsis>
        </template>
        <template slot="actions" slot-scope="text, record">
          <MoreButton>
            <a @click="handleDels(record)">查看</a>
            <template v-if="!isCopyInDesigns&&hideBuquBtn()">
              <template v-if="record.statusCode == 6&&(!record.promotionStatus || record.promotionStatus == 'PROMOTION_NO_PASS')">
                <a @click="handlePromotion(record)" v-btn-permission="'tm_005_tuy_06'">推优</a>
              </template>
              <template v-if="record.statusCode == 0 || record.statusCode == 7">
                <a @click="handleEdit(record)" v-btn-permission="'tm_005_out_05'">销项</a>
              </template>
              <template v-if="record.statusCode == 6">
                <BasePrintButton v-btn-permission="'tm_005_pri_07'"
                                 button-type="a"
                                 :before-print-fn="()=>handlePrint(record)"
                ></BasePrintButton>
              </template>
              <template v-if="record.statusCode == 2">
                <a @click="handleAcceptance(record)">验收</a>
              </template>
            </template>
            <template v-if="record.statusCode == 3 || record.statusCode == 4 || record.statusCode == 5 || record.statusCode == 6 || record.statusCode == 7">
              <a @click="handleApproveDetail(record)">审批信息</a>
            </template>
          </MoreButton>
        </template>
      </list-table>
      <ApprovalDetails
        :title="'审批详情'"
        :visible="visibleApprove"
        multiple
        :tab-list="tabList"
        @cancel="visibleApprove = false"
        @confirm="visibleApprove = false"
      />
    </a-card>
  </base-breadcrumb>
</template>

<script>
import moment from 'moment'
import { Ellipsis } from '@/components'
import BasePrintButton from '@/components/BasePrintButton' // 打印按钮
import {
  getImplementationList,
  getImplementationTotal,
  getImplementationBenefitTotal,
  applyPromotion,
  applyPromotionPrint
} from '@/api/project/implementation'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import ApprovalDetails from '@/components/approvalDetails'
import { optimization } from '@/api/project/optimization'

const columns = [
  {
    title: '序号',
    width: '65px',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '双优化立项编号',
    dataIndex: 'optimizationNum'
  },
  {
    title: '双优化立项名称',
    dataIndex: 'optimizationName'

  },
  {
    title: '实施项目',
    dataIndex: 'projectName'
  },
  {
    title: '分公司',
    dataIndex: 'branch'
  },
  {
    title: '优化类型',
    dataIndex: 'typeName',
  },
  {
    title: '详细分类',
    dataIndex: 'classification'

  },
  {
    title: '计划实施时间',
    dataIndex: 'startTime'
  },
  {
    title: '等级',
    width: '110px',
    dataIndex: 'level'
  },
  {
      title: '预测增效效益',
      dataIndex: 'increaseBenefit'
    },
    {
      title: '预测减亏效益',
      dataIndex: 'reduceBenefit'
    },
    {
      title: '优化类别',
      dataIndex: 'optimizeStageName'
    },
    {
      title: '立项效益(万元)',
      dataIndex: 'forecastBenefit'
    },
    {
      title: '项目业态',
      dataIndex: 'projectFormatName'
    },
    {
      title: '项目设计管理类别',
      dataIndex: 'designName'
    },
    {
      title: '专业类别',
      dataIndex: 'professionalTypeName'
    },
    {
      title: '设计阶段',
      dataIndex: 'designStageName'
    },
    {
      title: '工程类别',
      dataIndex: 'projectType'
    },
    {
      title: '分部',
      dataIndex: 'part'
    },
    {
      title: '创新',
      dataIndex: 'isNew'
    },
  {
    title: '实施效益',
    width: '110px',
    dataIndex: 'implementBenefits'
  },
  {
    title: '实际实施时间',
    width: '110px',
    dataIndex: 'implementDate'
  },
  {
    title: '销项状态',
    width: '110px',
    dataIndex: 'statusName'
  },
  {
    title: '推优状态',
    width: '110px',
    dataIndex: 'promotionStatusName'
  },
  {
    title: '操作',
    dataIndex: 'actions',
    width: '150px',
    fixed: 'right',
    scopedSlots: { customRender: 'actions' }
  }
]
const customColumns =  [
  {
    title: '序号',
    width: '65px',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '双优化立项编号',
    dataIndex: 'optimizationNum'
  },
  {
    title: '双优化立项名称',
    dataIndex: 'optimizationName'

  },
  {
    title: '实施项目',
    dataIndex: 'projectName'
  },
  {
    title: '分公司',
    dataIndex: 'branch'
  },
  {
    title: '优化类型',
    dataIndex: 'typeName',
  },
  {
    title: '详细分类',
    dataIndex: 'classification'

  },
  {
    title: '计划实施时间',
    dataIndex: 'startTime'
  },
  {
    title: '计划结束时间',
    dataIndex: 'endTime'
  },
  {
    title: '提出人',
    dataIndex: 'proposer'
  },
  {
    title: '拟实施人',
    dataIndex: 'implementer'
  },
  {
    title: '预测增效效益',
    dataIndex: 'increaseBenefit'
  },
  {
    title: '预测减亏效益',
    dataIndex: 'reduceBenefit'
  },
  {
    title: '优化类别',
    dataIndex: 'optimizeStageName'
  },
  // {
  //   title: '立项效益(万元)',
  //   dataIndex: 'forecastBenefit'
  // },
  {
    title: '项目业态',
    dataIndex: 'projectFormatName'
  },
  {
    title: '项目设计管理类别',
    dataIndex: 'designName'
  },
  {
    title: '专业类别',
    dataIndex: 'professionalTypeName'
  },
  {
    title: '设计阶段',
    dataIndex: 'designStageName'
  },
  {
    title: '工程类别',
    dataIndex: 'projectType'
  },
  {
    title: '分部',
    dataIndex: 'part'
  },
  {
    title: '创新',
    dataIndex: 'isNew'
  },
  {
    title: '立项效益（万元）',
    width: '110px',
    dataIndex: 'forecastBenefit'
  },
  {
    title: '等级',
    width: '110px',
    dataIndex: 'level'
  },
  {
    title: '实施效益（万元）',
    width: '110px',
    dataIndex: 'implementBenefits'
  },
  {
    title: '实际实施时间',
    width: '110px',
    dataIndex: 'implementDate'
  },
  {
    title: '销项状态',
    width: '110px',
    dataIndex: 'statusName'
  },
  {
    title: '推优状态',
    width: '110px',
    dataIndex: 'promotionStatusName'
  },
]
export default {
  name: 'a' + Date.now(),
  components: {
    Ellipsis,
    ApprovalDetails,
    BasePrintButton
  },
  data() {
    this.columns = columns
    return {
      isCopyInDesigns: location.href.includes('designs'),//是否是复制到设计中，如果是直接把操作按钮false掉
      businessId: '',
      visibleApprove: false,
      customColumns,
      searchArr: [
        // 搜索框
        {
          name: '双优化立项名称',
          value: 'optimizationName',
          type: 'input'
        },
        {
          name: '优化类型 ',
          value: 'type',
          type: 'select',
          data: [
            { label: '设计优化', value: '111' },
            { label: '施工组织设计(方案)优化', value: '222' }
          ]
        },
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        {
          name: '销项状态',
          value: 'statusCode',
          type: 'select',
          data: [
            { label: '未实施', value: '1' },
            { label: '实施中', value: '0' },
            { label: '已实施未验收', value: '2' },
            { label: '验收审核中', value: '3' },
            { label: '已实施已验收', value: '4' },
            { label: '销项审核中', value: '5' },
            { label: '销项审核不通过', value: '7' },
            { label: '销项审核通过', value: '6' }
          ]
        },
        {
          name: '计划实施时间',
          value: 'time',
          type: 'dateRange'
        },
        {
          name: '推优状态',
          value: 'promotionStatus',
          type: 'select',
          data: [
            { label: '推优审核中', value: '0' },
            { label: '推优审核不通过', value: '2' },
            { label: '推优审核通过', value: '1' }
          ]
        },
        {
          name: '等级',
          value: 'level',
          type: 'select',
          data: [
            { label: '一等', value: '一等' },
            { label: '二等', value: '二等' },
            { label: '三等', value: '三等' },
            { label: '四等', value: '四等' },
            { label: '五等', value: '五等' }
          ]
        },
      ],
      titleName: '',

      expand: true,   // 展开详情汇总信息

      // 高级搜索 展开/关闭
      advanced: false,
      benefitTotals: {},
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        this.getImplementationTotal()
        this.getImplementationBenefitTotal()
        return new Promise((resolve)=>{
          getImplementationList(requestParameters).then(res=>{
            res.data.forEach(item=>{
              if(item.implementStartDate) {
                item.implementDate = item.implementStartDate+ '至' + item.implementEndDate
              }
              if(!item.promotionStatusName) {
                item.promotionStatusName = '-'
              }
              if(item.promotionStatus == 0) {
                item.promotionStatus = '0'
              }
            })
            resolve(res)
          })
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      totalDels: {},
      tabList: []
    }
  },
  computed: {
    // rowSelection() {
    //   return {
    //     selectedRowKeys: this.selectedRowKeys,
    //     selectedRows: this.selectedRows,   // 当前行所有数据
    //     onChange: this.onSelectChange
    //   }
    // }
  },
  methods: {
    /**
     * @description: 打印
     * @param {void}
     * @return {void}
     */
    handlePrint(record) {
      return applyPromotionPrint({ businessId: record.optimizationId, templateCode:'CSCEC8XN-TD-B20017' }).then(({ data: { content } }) => {
        return content
      })
    },
    // 推优
    handlePromotion(record) {
      let that = this
      this.$confirm({
        title: '提示',
        content: "请确认是否要将该双优化项推优?",
        onOk() {
        return applyPromotion({applyId: record.optimizationId}).then(res=>{
          that.$message.success('提交成功')
          that.$refs.table.refresh(true)
          })
        },
        onCancel() {},
      });
    },
    handleApproveDetail(record) {
      if(record.statusCode == 6) {
        this.tabList = [
          {
            name: '销项流程',
            value: record.optimizationId+'_C_A_N'
          },
          {
            name: '推优流程',
            value: record.optimizationId+'_P_R_O'
          },
        ]
      } else if(record.statusCode == 3 || record.statusCode == 4){
        this.tabList = [
          {
            name: '验收流程',
            value: record.optimizationId+'_A_C_C'
          }
        ]
      } else {
        this.tabList = [
          {
            name: '销项流程',
            value: record.optimizationId+'_C_A_N'
          }
        ]
      }

      this.visibleApprove = true
    },
    toggle() {
      this.expand = !this.expand
      this.getImplementationBenefitTotal()
    },
    getImplementationBenefitTotal() {
      getImplementationBenefitTotal(this.queryParam).then(res => {
        if (res.code == 200) {
          this.benefitTotals = res.data
        }
      })
    },
    search(val) {
      this.queryParam = val
      if (this.queryParam.time&&this.queryParam.time.length) {
        this.queryParam.startTime = this.formatDate(this.queryParam.time[0])
        this.queryParam.endTime = this.formatDate(this.queryParam.time[1])
      } else {
        delete this.queryParam.startTime
        delete this.queryParam.endTime
        delete this.queryParam.time
      }

      this.$refs.table.refresh(true)
      // this.getImplementationTotal()
      // this.getImplementationBenefitTotal()

    },
    getImplementationTotal() {
      getImplementationTotal(this.queryParam).then(res => {
        this.totalDels = res.data
      })
    },

    handleDels(val) {
      this.$router.push({
        path: '/project/implementation/sales',
        query: { id: val.optimizationId, statusCode: val.statusCode, isEdit: 0, view: 'view' }
      })
    },

    handleEdit(val) {
      this.$router.push({
        path: '/project/implementation/sales',
        query: { id: val.optimizationId, isEdit: 1, statusCode: val.statusCode }
      })
    },

    handleAcceptance(val) {
      this.$router.push({
        path: '/project/implementation/acceptance',
        query: { id: val.optimizationId, isEdit: 1, statusCode: val.statusCode, reviewButton: val.reviewButton }
      })
    },

    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>

<style scoped type="text/css" lang="less">
.wrapper {
  overflow: hidden;
  padding: 10px 15px;
  margin-bottom: 20px;
}
.row {
  display: flex;
}
.row .name {
  border: 1px solid #686868;
  height: 35px;
  line-height: 35px;
  width: 20%;
  margin-right: -1px;
  margin-top: -1px;
  text-align: center;
  font-weight: bold;
  display: block;
}

</style>
